package com.jiangxiaofei.citycloud.util;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.FleaGoodAdapter;
import com.jiangxiaofei.citycloud.bean.FleaGoodBean;

import java.util.ArrayList;
import java.util.List;

public class TabFragment extends Fragment {
    public static final String TITLE = "title";
    private String mTitle = "Defaut Value";
    private RecyclerView mRecyclerView;
    private FleaGoodAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(TITLE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_fragment_tab, container, false);
        mRecyclerView = view
                .findViewById(R.id.id_stickynavlayout_innerscrollview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        adapter = new FleaGoodAdapter(R.layout.item_flea_good, goodsList);
        mRecyclerView.setAdapter(adapter);
        return view;

    }

    List<FleaGoodBean> goodsList = new ArrayList<>();

    /**
     * 初始化数据
     */
    public void initData(List<FleaGoodBean> goodsList) {
        this.goodsList.clear();
        this.goodsList.addAll(goodsList);
    }

    /**
     * 加载数据
     */
    public void setData(List<FleaGoodBean> goodsList) {
        this.goodsList.addAll(goodsList);
        adapter.notifyDataSetChanged();
    }

    public static TabFragment newInstance(String title) {
        TabFragment tabFragment = new TabFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TITLE, title);
        tabFragment.setArguments(bundle);
        return tabFragment;
    }

}
